<?php namespace App\Http\Controllers;

use App\Example;
use App\Exceptions\NotFoundException;
use App\Facilitator;
use App\Fields;
use App\Msg;
use App\Org;
use App\Pusers;
use App\Skills;
use App\Sys;
use App\User;
use Illuminate\Auth\Guard;

class HomeController extends Controller {

	/*
	|--------------------------------------------------------------------------
	| Home Controller
	|--------------------------------------------------------------------------
	|
	| This controller renders your application's "dashboard" for users that
	| are authenticated. Of course, you are free to change or remove the
	| controller as you wish. It is just here to get your app started!
	|
	*/

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
          $webInfo = Sys::find(1);
		return view()->share('webinfo',$webInfo);
	}

	/**
	 * Show the application dashboard to the user.
	 *
	 * @return Response
	 */
	public function index()
	{
		return view('home');
	}

	public function login(){
		$userCount = User::all()->count();
		return view('auth.login',['userCount'=>$userCount]);
	}

	public function register(){
		$userCount = User::all()->count();
		return view('auth.register',['userCount'=>$userCount]);
	}

	public function forgetPassword(){
		$userCount = User::all()->count();
		return view('auth.forget-password',['userCount'=>$userCount]);
	}

	public function about(){
		return view('about');
	}

	public function agreeMent(){
		return view('auth.user-agreement');
	}

	public function createFacilitator(Guard $guard){
		if($guard->guest())
			return redirect(route('Home.Auth.login'));

		/** @var User $user */
		$user = $guard->user();
		if(!$user)
			throw new NotFoundException(NotFoundException::UserNotFound);

		$facInfo = $user->facilitator;
		if(!$facInfo){
			$facInfo = new Facilitator();
			$facInfo->user()->associate($user);
		}

		$fields = Fields::all();
		$skills = Skills::all();

		$orgInfo = $facInfo->org;
		if($orgInfo){
			$field = $orgInfo->fields()->lists('field_id');
			$orgInfo['field'] = $field;
		}

		if($orgInfo){
			$skill = $orgInfo->skills()->lists('skill_id');
			$orgInfo['skill'] = $skill;
		}

		$examInfos = '';
		if($orgInfo)
			$examInfos = Example::whereOrgId($orgInfo->id)->paginate(10);

		return view('service.create-facilitator',[
			'user'   => $user,
			'facInfo'=> $facInfo,
			'fields' => $fields,
			'skills' => $skills,
			'orgInfo'=> $orgInfo,
			'examInfos' => $examInfos
		]);
	}

	public function createPop(Guard $guard){
		if($guard->guest())
			return redirect(route('Home.Auth.login'));

		/** @var User $user */
		$user = $guard->user();
		if(!$user)
			throw new NotFoundException(NotFoundException::UserNotFound);

		$facInfo = $user->facilitator;
		if(!$facInfo){
			$facInfo = new Facilitator();
			$facInfo->user()->associate($user);
		}
		return view('service.create-pop',[
			'user'   => $user,
			'facInfo'=> $facInfo
		]);
	}

	public function userCenter(Guard $guard){
		if($guard->guest())
			return redirect(route('Home.Auth.login'));

		/** @var User $user */
		$user = $guard->user();
		if(!$user)
			throw new NotFoundException(NotFoundException::UserNotFound);

		/** @var Facilitator $facInfo */
		$facInfo = $user->facilitator;

		if(!$facInfo){
            $facInfo = new Facilitator();
            $facInfo->user()->associate($user);
        }

		$fields = Fields::all();
		$skills = Skills::all();

		$orgInfo = $facInfo->org;
		if($orgInfo){
			$field = $orgInfo->fields()->lists('field_id');
			$orgInfo['field'] = $field;
		}

		if($orgInfo){
			$skill = $orgInfo->skills()->lists('skill_id');
			$orgInfo['skill'] = $skill;
		}

		$examInfos = '';
		if($orgInfo)
			$examInfos = Example::whereOrgId($orgInfo->id)->paginate(10);

		/** 获取上级信息 */
		$parentInfo = $facInfo?$facInfo->followParent()->get():null;

		/** 获取下级信息 */
		$childs = $facInfo?$facInfo->followChild()->get():null;
		foreach($childs as $child){
			$orders = $child->Orders;
			$reward = 0;
			foreach($orders as $order){
				$reward = $order->reward + $reward;
			}
			$child['rewards']=$reward;
		}

		/** 获取系统消息 */
		$msgs = Msg::whereUserId($user->id)->get();
		return view('service.user-center',[
			'user'   => $user,
			'facInfo'=> $facInfo,
			'fields' => $fields,
			'skills' => $skills,
			'orgInfo'=> $orgInfo,
			'examInfos' => $examInfos,
			'parent' => $parentInfo,
			'childs' => $childs,
			'msgs'   => $msgs
		]);
	}

	/** 登出 */
	public function signOut(Guard $guard)
	{
		$guard->logout();
		return redirect(route('Home'));
	}
}
